package com.ysstech.finance.service.impl;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysstech.common.entity.PageBean;
import com.ysstech.common.service.DicvalueService;
import com.ysstech.finance.entity.Voucher;
import com.ysstech.finance.enums.FinanceEnum;
import com.ysstech.finance.mapper.VoucherMapper;
import com.ysstech.finance.service.VoucherService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lishuangliang
 * @since 2021-03-10
 */
@Service
public class VoucherServiceImpl extends ServiceImpl<VoucherMapper, Voucher> implements VoucherService {
    @Autowired
    private VoucherMapper voucherMapper;
    @Autowired
    protected DicvalueService dicValueService;

    /**
     * 查询凭证列表信息
     *
     * @param voucher
     * @param pageBean
     * @return
     * @throws Exception
     */
    public PageBean<Voucher> queryAllVoucher(Voucher voucher, PageBean pageBean) throws Exception {
        QueryWrapper<Voucher> wrapper = this.getQueryWrapper(voucher);
        IPage pageVoucher = voucherMapper.queryVoucher(new Page<>(pageBean.getPageNum(), pageBean.getPageSize()), wrapper);
        this.switchedListVoucher(pageVoucher.getRecords());
        return new PageBean<>(pageVoucher);
    }

    /**
     * 查询凭证列表信息
     *
     * @param voucher
     * @return
     * @throws Exception
     */
    public List<Voucher> queryAllVoucher(Voucher voucher) throws Exception {
        QueryWrapper<Voucher> wrapper = new QueryWrapper<>();
        wrapper.eq("NUM", voucher.getNum());
        wrapper.eq("ACCOUNT_ID", voucher.getAccountId());
        wrapper.orderByAsc("SEQUENCE");
        return voucherMapper.queryVoucher(wrapper);
    }

    /**
     * 类型，机构等中文的转换
     *
     * @param list
     * @return
     * @throws Exception
     */
    public List<Voucher> switchedListVoucher(List<Voucher> list) throws Exception {
        Map<String, String> mapCurrency = dicValueService.getCacheDicValueMap(FinanceEnum.CURRENCY.getCode());
        for (Voucher voucher : list) {
            if (null == voucher) {
                continue;
            }
            if (null != mapCurrency && mapCurrency.containsKey(voucher.getCurrency())) {
                voucher.setCurrency(mapCurrency.get(voucher.getCurrency()));
            }
        }
        return list;
    }

    /**
     * 查询条件
     *
     * @param voucher
     * @return
     * @throws Exception
     */
    private QueryWrapper<Voucher> getQueryWrapper(Voucher voucher) throws Exception {
        QueryWrapper<Voucher> wrapper = new QueryWrapper<>();
        if (null == voucher) {
            return wrapper;
        }
        if (StringUtils.isNotBlank(voucher.getAccountId())) {
            List<String> list = JSONArray.parseArray(voucher.getAccountId(), String.class);
            if (!CollectionUtils.isEmpty(list)) {
                wrapper.in("ACCOUNT_ID", list);
            }
        }
        if (StringUtils.isNotBlank(voucher.getSelfdate())) {
            wrapper.eq("FDATE", voucher.getSelfdate());
        }
        if (StringUtils.isNotBlank(voucher.getRemark())) {
            wrapper.like("REMARK", voucher.getRemark());
        }
        if (StringUtils.isNotBlank(voucher.getFtype())) {
            List<String> listtype = JSONArray.parseArray(voucher.getFtype(), String.class);
            if (!CollectionUtils.isEmpty(listtype) && StringUtils.isNotBlank(listtype.get(0))) {
                wrapper.in("FTYPE", listtype);
            }
        }
        if (StringUtils.isNotBlank(voucher.getSource())) {
            List<String> list = JSONArray.parseArray(voucher.getSource(), String.class);
            if (!CollectionUtils.isEmpty(list) && StringUtils.isNotBlank(list.get(0))) {
                wrapper.in("SOURCE", list.get(0));
            }
        }
        wrapper.orderByAsc("NUM,SOURCE,FTYPE,SEQUENCE");
        return wrapper;
    }

    /**
     * 查询余额表需要统计的凭证
     *
     * @param voucher
     * @return
     */
    public List<Voucher> queryVoucherBalance(Voucher voucher) throws Exception {
        return voucherMapper.queryVoucherBalance(voucher);
    }


}
